<?php

include_once dirname(dirname(__FILE__)) . '/include/common.php';
$core = new core();
$product_id = isset($_POST["product_id"]) ? intval($_POST["product_id"]) : 0;
$product_size = isset($_POST["product_size"]) ? addslashes(trim($_POST["product_size"])) : '';
$product_color = isset($_POST["product_color"]) ? addslashes(trim($_POST["product_color"])) : '';
$folder_temp = isset($_POST["folder_temp"]) ? addslashes(trim($_POST["folder_temp"])) : '';
$file_main_photo_temp = isset($_POST["file_main_photo_temp"]) ? addslashes(trim($_POST["file_main_photo_temp"])) : '';
$product = $core->getProductById($product_id);
if (!$product) {
    echo json_encode(array('type' => 1, 'msg' => "Sản phẩm không tồn tại!"));
    die;
}

$message = '';
$update = '';
if ($product_color != '') {

    if ($file_main_photo_temp != "") {
        $new_path_extra_photo = dirname(dirname(__FILE__)) . DS . "photo" . DS . "extra_photo" . DS;
        $new_path_extra_photo_thumb = dirname(dirname(__FILE__)) . DS . "photo" . DS . "extra_photo" . DS . "thumb_extra_photo" . DS;
        $new_photo_name = $product_color . "_" . $product->product_code . "_" . $product->manufacture;
        $new_photo_name = $core->convert_string_friendly($new_photo_name) . ".jpg";
        $check_save_extra_photo = rename(dirname(dirname(__FILE__)) . DS . "tmp_photo" . DS . $folder_temp . DS . $file_main_photo_temp, $new_path_extra_photo . $new_photo_name);
        $check_save_extra_thumb_photo = rename(dirname(dirname(__FILE__)) . DS . "tmp_photo" . DS . $folder_temp . DS . "200_" . $file_main_photo_temp, $new_path_extra_photo_thumb . $new_photo_name);
        if (!$check_save_extra_photo || !$check_save_extra_thumb_photo) {
            echo json_encode(array('type' => 1, 'msg' => "Lưu ảnh bị lỗi!"));
            die;
        }
        if ($product->extra_photo == "") {
            $extra_photo_list = $new_photo_name;
            $update.="extra_photo='" . $extra_photo_list . "',";
        } else {
            $arr_extra_photo = explode(",", $product->extra_photo);
            if (!in_array($new_photo_name, $arr_extra_photo)) {
                $extra_photo_list = $product->extra_photo . "," . $new_photo_name;
                $update.="extra_photo='" . $extra_photo_list . "',";
            }
        }
    }
    $color = '';
    if ($product->product_color != "") {
        $arr_color = explode(",", $product->product_color);

        if (!in_array($product_color, $arr_color)) {
            $color = $product->product_color . "," . addslashes($product_color);
        } else {
            $color = $product->product_color;
        }
    } else {
        $color = addslashes($product_color);
    }
    $update.="product_color='" . $color . "',";
}
if ($product_size != '') {
    $size = '';
    if ($product->product_size != "") {
        $arr_size = explode(",", $product->product_size);
        if (!in_array($product_size, $arr_size)) {
            $size = $product->product_size . "," . addslashes($product_size);
        } else {
            $size = $product->product_size;
        }
    } else {
        $size = addslashes($product_size);
    }

    $update.="product_size='" . $size . "',";
}
$update = substr($update, 0, -1);
if ($update != "") {
    $core->db->query("UPDATE zen_product SET $update 
             WHERE product_id=$product_id");

    $message = "Update thông tin sản phẩm thành công!";
    echo json_encode(array('type' => 0, 'msg' => $message));
    die;
} else {
    $message = "Không có gì được update!";
    echo json_encode(array('type' => 1, 'msg' => $message));
    die;
}
?>